<?php

namespace App\Models\SystemManagement;

use App\Models\Api\Files;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Hash;
use Tymon\JWTAuth\Contracts\JWTSubject;
use App\Models\Constant\Fields;

class User extends Authenticatable implements JWTSubject
{
    use Notifiable, SoftDeletes;
    static $SYSTEM = 'App\Models\SystemManagement';
    protected $table = 'users';
    protected $guarded = ['role_id'];
    protected $hidden = ['deleted_at', 'created_at', 'updated_at', 'remember_token'];
    public $incrementing = false;

    static function add($data)
    {
        $data['password'] = Hash::make($data['password']);
        $param = new self();
        $param->create($data);
        return $param;
    }

    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        // TODO: Implement getJWTIdentifier() method.
        return $this->getKey();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        // TODO: Implement getJWTCustomClaims() method.
        return [];
    }

    /**
     * @return mixed
     * 获取部门数据
     */
    public static function getDataToArr()
    {
        $query = self::getTableDataDataToArr(new Department());
        return $query;
    }

    //查看用户
    public function employees()
    {
        return $this->belongsTo(self::$SYSTEM . '\Employee', Fields::EMPLOYEE_ID, Fields::ID)
            ->with(['department', 'post'])
            ->select('id', 'name', 'phone', 'department_id', 'post_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     * 用户角色关系
     */
    public function userRole()
    {
        return $this->hasMany(self::$SYSTEM . '\UserRole', Fields::USER_ID, Fields::ID)->with('role');
    }

    //查看头像
    public function file()
    {
        return $this->belongsTo(Files::class, 'picture');
    }

    // /**
    //  * 用户对应的微信公众号信息 一对一关系
    //  * @return \Illuminate\Database\Eloquent\Relations\hasOne
    //  */
    // function weiChatInfo()
    // {
    //     return $this->hasOne(\App\Models\Garbage\WeichatUser::class,'user_id');
    // }

//    /**
//     * @return \Illuminate\Database\Eloquent\Relations\HasMany
//     * 垃圾分类 用户积分管理 投递次数
//     */
//    public function GarbagePostNum(){
//        return $this->hasMany(GarbagePoint::class,'user_id','id');
//    }


    public function avatar()
    {
        return $this->belongsTo(Files::class, 'picture');
    }

}

